﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public static class ActionXMLReader
    {
        public static List<ActionHelper> loadAllAction(string xmlActionFile)
        {
            //<steps>
            //  <step name="MoveAction">
            //    <input>
            //      <position>-500,200</position>
            //      <targetPosition>100,200</targetPosition>
            //      <beginSpeed>10</beginSpeed>
            //      <acceleration>4.0</acceleration>
            //    </input>
            //    <output></output>
            //  </step>
            //  <step name="MoveAction">
            //    <input>
            //      <position>500,350</position>
            //      <targetPosition>150,350</targetPosition>
            //      <beginSpeed>12</beginSpeed>
            //      <acceleration>5.0</acceleration>
            //    </input>
            //    <output></output>
            //  </step>
            //  <step name="MoveAction">
            //    <input>
            //      <position>500,500</position>
            //      <targetPosition>200,500</targetPosition>
            //      <beginSpeed>14</beginSpeed>
            //      <acceleration>6.0</acceleration>
            //    </input>
            //    <output></output>
            //  </step>
            //  <step name="ScaleAction">
            //    <input>
            //      <finishScale>1.2</finishScale>
            //    </input>
            //  </step>
            //</steps>

            List<ActionHelper> result = new List<ActionHelper>();

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlActionFile);
                XmlElement root = doc.DocumentElement;

                // load steps
                XmlNodeList listStep = root.SelectNodes("//step");
                foreach (XmlNode node in listStep)
                {
                    if (((XmlElement)node).GetAttribute("name") == "MoveAction")
                    {
                        // node input
                        XmlNode eleInput = node.ChildNodes[0];
                        Vector2 pos = Vector2.Zero;
                        Vector2 tarpos = Vector2.Zero;
                        float beginspeed = 0;
                        float acc = 0;
                        foreach (XmlNode nodez in eleInput.ChildNodes)
                        {

                            if (nodez.Name == "position")
                            {
                                string[] strpos = nodez.InnerText.Split(',');
                                pos = new Vector2(float.Parse(strpos[0]), float.Parse(strpos[1]));
                            }
                            if (nodez.Name == "targetPosition")
                            {
                                string[] strtarpos = nodez.InnerText.Split(',');
                                tarpos = new Vector2(float.Parse(strtarpos[0]), float.Parse(strtarpos[1]));
                            }
                            if (nodez.Name == "beginSpeed")
                            {
                                beginspeed = float.Parse(nodez.InnerText);
                            }
                            if (nodez.Name == "acceleration")
                            {
                                acc = float.Parse(nodez.InnerText);
                            }


                        }
                        MoveAction action = new MoveAction(@"MoveAction" + result.Count.ToString(),null, tarpos, beginspeed, acc);
                        action.BeginPosition = pos;
                        result.Add(action);

                        // node output
                        XmlNode eleOutput = node.ChildNodes[1];
                    }
                    else if (((XmlElement)node).GetAttribute("name") == "ScaleAction")
                    {
                        // node input
                        XmlNode eleInputz = node.ChildNodes[0];               
                        foreach (XmlNode nodez in eleInputz.ChildNodes)
                        {
                            if (nodez.Name == "finishScale")
                            {
                                result.Add(new ScaleAction(null, float.Parse(nodez.InnerText)));
                            }
                        }
                        // node output
                        XmlNode eleOutput = node.ChildNodes[1];
                    }                    
                }
            }
            catch
            {
                return null;
            }

            return result;
        }
    }
}
